package ducct.footprint.ui;

import java.util.List;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import ducct.footprint.R;
import ducct.footprint.db.DatabaseBackgroundWorker;
import ducct.footprint.db.DbTask;
import ducct.footprint.entity.MyFootprint;
import ducct.footprint.location.LocationOverlay;
import ducct.footprint.location.MyLocationCallback;
import ducct.footprint.location.MyLocationListener;
import ducct.footprint.utils.Global;
import ducct.footprint.utils.Utils;

public class CreateFootprintActivity extends MapActivity {

	private MyFootprint myFootprint;
	private Location currentLocation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		myFootprint = (MyFootprint) getIntent().getExtras().get(Global.MY_FOOTPRINT);
		
		// loading
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.loading);
        setProgressBarVisibility(true);
		
		// request current location info
		currentLocation = MyLocationListener.getInstance().getCurrentLocation();
		if (currentLocation == null) {
			MyLocationListener.getInstance().waitForLocation(callback);
		} else {
			onGotCurrentLocation(currentLocation);
		}
	}
	
	private MyLocationCallback callback = new MyLocationCallback() {
		
		@Override
		public void onGotCurrentLocation(final Location location) {
			runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					CreateFootprintActivity.this.onGotCurrentLocation(location);
				}
			});
		}
		
		@Override
		public void onCannotGetLocation() {
			runOnUiThread(new Runnable() {
				public void run() {
//					Toast.makeText(getApplicationContext(), "Can't get your current location", 
//							Toast.LENGTH_SHORT).show();
					setContentView(R.layout.error_create_footprint);
				}
			});
		}
	};
	
	private void onGotCurrentLocation(Location location) {
		myFootprint.lat = location.getLatitude();
		myFootprint.lng = location.getLongitude();
		
		displayFootprintInfo();
	}
	
	private void displayFootprintInfo() {
		setContentView(R.layout.create_footprint);
		
		((TextView) findViewById(R.id.txtTimestamp)).setText(
				Utils.getCurrentMinuteAndSecond(myFootprint.timestamp));
		
		final EditText txtName = (EditText) findViewById(R.id.txtLocationName);
		final EditText txtNote = (EditText) findViewById(R.id.txtNote);
		
		displayMap();
		
		Button btnSave = (Button) findViewById(R.id.btnSave);
		btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				myFootprint.locationName = txtName.getText().toString();
				myFootprint.note = txtNote.getText().toString();
				
				saveFootprint();
				
				finish();
				startActivity(new Intent(CreateFootprintActivity.this, MyFootprintListActivity.class));
			}
		});
	}
	
	private MapView mapView;
	private LocationOverlay locationOverlay;
	private MapController mapController;
	private void displayMap() {
		// map view
		mapView = (MapView) findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);

		// overlay
		List<Overlay> listOfOverlays = mapView.getOverlays();
		listOfOverlays.clear();
		locationOverlay = new LocationOverlay();
		locationOverlay.point = new GeoPoint(
				(int) (myFootprint.lat * 1E6),(int) (myFootprint.lng * 1E6));
		listOfOverlays.add(locationOverlay);

		// controller
		mapController = mapView.getController();
		mapController.setZoom(16);
		
		mapController.setCenter(locationOverlay.point);
		mapView.invalidate();	// TODO is need to invalidate map?
	}
	private void saveFootprint() {
		DatabaseBackgroundWorker.queueDbTask(new DbTask() {
			
			@Override
			public void loadData() {
				Global.db.saveMyFootprint(myFootprint);
			}
		});
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
